//
//  SaomiaoViewController.m
//  HengHuaSupervision
//
//  Created by XD-XY on 3/24/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "SaomiaoViewController.h"
#import "XDTools.h"
#import "XDHeader.h"
#import "PressViewController.h"

@interface SaomiaoViewController ()

@end

@implementation SaomiaoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    [self initSaoMiaoViews];
    [readView start];
    [saomiaoline setHidden:NO];
    [self startsaomiaoAnimation];

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [self closeCameraScanner];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.contentView.frame = CGRectMake(0, 0, mUI_Screen_Height, mUI_Screen_Width);
    
    [self initSaoMiaoViews];
}

#pragma mark =================UI Create=======================
-(void)initSaoMiaoViews
{
    readView = [[ZBarReaderView alloc] init];
    readView.frame =self.contentView.bounds;
    [readView willRotateToInterfaceOrientation:[[UIApplication sharedApplication] statusBarOrientation] duration:[[UIApplication sharedApplication] statusBarOrientationAnimationDuration]];
    readView.readerDelegate = self;
    //关闭闪光灯
    readView.torchMode =0;
 
//      //模拟器
    CGRect scanMaskRect = CGRectMake(638/2.0f,352/2.0f,770/2.0f,770/2.0f);
//
//    if (TARGET_IPHONE_SIMULATOR){
//        ZBarCameraSimulator *cameraSimulator
//        = [[ZBarCameraSimulator alloc]initWithViewController:self];
//        cameraSimulator.readerView = readeerView;
//    }
//    //设置扫描区域
    readView.scanCrop = [self getScanCrop:scanMaskRect readerViewBounds:readView.bounds];
    
    [self.contentView addSubview:readView];
    
    //背景图片
    UIImageView * bgView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
    bgView.image = mSaomiao_Bg_Img;
    [readView addSubview:bgView];
    
    //标签编号Label
    self.tagNumberLabel = [UILabel initLabel:CGRectMake(0, 150/2.0f, 1024, 20) textFont:mSaomiao_Title_Font textColor:RGBA(171, 171, 171) textAlignment:NSTextAlignmentCenter];
    _tagNumberLabel.text = @"将二维码对入取景框，即可自动扫描";
    [readView addSubview:_tagNumberLabel];
    
    //扫描的线
    saomiaoline = [[UIImageView alloc] initWithImage:mSaomiao_line_Img];
    saomiaoline.frame = CGRectMake(590/2+25,304/2+25, mSaomiao_line_Img.size.width-1, mSaomiao_line_Img.size.height);
    [readView addSubview:saomiaoline];
    
    //取消按钮
    cancelBtn = [UIButton initButton:CGRectMake(763/2.0f+5.5, 1302/2.0f,500/2.0f, 100/2.0f) btnNorImage:mSaomiao_cancel_Normal_Img btnPressBtn:mSaomiao_cancel_Highlight_Img btnTitle:nil titleColor:nil titleFont:nil];
    [cancelBtn addTarget: self action:@selector(cancelClick) forControlEvents:UIControlEventTouchDown];
    [readView addSubview:cancelBtn];

}

-(void)startsaomiaoAnimation
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:3];
    [UIView setAnimationRepeatCount:FLT_MAX];
    [UIView setAnimationRepeatAutoreverses:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    saomiaoline.frame = CGRectMake(590/2+25,304/2+25+770/2.0f-5, mSaomiao_line_Img.size.width-1, mSaomiao_line_Img.size.height);
    [UIView commitAnimations];
}

//返回
-(void)cancelClick
{
//    PressViewController * pressVC = [[PressViewController alloc] init];
//   [self.navigationController pushViewController:pressVC animated:YES];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)readerView:(ZBarReaderView *)readerView didReadSymbols:(ZBarSymbolSet *)symbols fromImage:(UIImage *)image
{
    NSString * symbolStr;
    for (ZBarSymbol *symbol in symbols) {
        symbolStr = symbol.data;
//        symbolStr = [NSString stringWithCString:[symbolStr cStringUsingEncoding:NSShiftJISStringEncoding] encoding:NSUTF8StringEncoding];
        
        DDLOG(@"%@", symbolStr);
        break;
    }
    [saomiaoline setHidden:YES];
    [readerView stop];
    
    //判断是否是合法标签
    if([self judgeTags:symbolStr]){
        PressViewController * pressVC = [[PressViewController alloc] init];
        pressVC.titleString = symbolStr;
        [self.navigationController pushViewController:pressVC animated:YES];
    }else{
        [saomiaoline setHidden:YES];
    }
}

//关闭扫描页面
-(void)closeCameraScanner
{
    if (nil!=readView){
        [readView removeFromSuperview];
    }
    [self.view endEditing:YES];
}


//可设置扫描区域
-(CGRect)getScanCrop:(CGRect)rect readerViewBounds:(CGRect)readerViewBounds
{
    CGFloat x,y,width,height;
    
    x = rect.origin.x / readerViewBounds.size.width;
    y = rect.origin.y / readerViewBounds.size.height;
    width = rect.size.width / readerViewBounds.size.width;
    height = rect.size.height / readerViewBounds.size.height;
    
    return CGRectMake(x, y, width, height);
}

//判断是否是合法标签
-(BOOL)judgeTags:(NSString *)sender
{
    FMDatabase * db = [XDTools getDb];
    if (![db open]){
        DDLOG(@"判断标签是否合法-->标签库-->数据库打开失败");
        return NO;
    }
    
    FMResultSet * rs = [db executeQuery:@"SELECT * FROM T_TRACE_TAGS WHERE code = ?",sender];
    if (![rs next]){
        _tagNumberLabel.text = [NSString stringWithFormat:@"标签编码:%@ 扫描失败，该标签是非法标签",sender];
        return NO;
    }else{
        FMResultSet * rs2 = [db executeQuery:@"SELECT * FROM T_TRACE_ASSERTS_TAG WHERE tag_code = ?",sender];
        if ([rs2 next]){
            _tagNumberLabel.text = [NSString stringWithFormat:@"标签编号:%@ 扫描失败，该标签是已贴标签",sender];
            return NO;
        }else{
            return YES;
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
